package com.cn.kehong.service;

import com.cn.kehong.api.IProductThinPeriodService;
import com.cn.kehong.dao.ProductThinPeriodMapper;
import com.cn.kehong.domain.ProductThinPeriod;
import com.cn.kehong.domain.ProductThinPeriodExample;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductThinPeriodServiceImpl implements IProductThinPeriodService {

    @Autowired
    private ProductThinPeriodMapper productThinPeriodMapper;

    @Override
    public List<ProductThinPeriod> getProductThinPeriodList(QueryForm queryForm) throws Exception {
        int count = productThinPeriodMapper.selectProductThinPeriodCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductThinPeriod> productThinPeriods = productThinPeriodMapper.selectProductThinPeriods(queryForm);
        productThinPeriods.get(0).setCount(count);
        return productThinPeriods;
    }

    @Override
    public List<ProductThinPeriod> getProductThinPeriodByLineStatus() throws Exception {
        return productThinPeriodMapper.selectProductThinPeriodByLineStatus();
    }

    @Override
    public List<ProductThinPeriod> getProductThinPeriodListByCodes(List<String> productCodes) throws Exception {
        if(CollectionsUtil.isEmpty(productCodes)){
            return Lists.newArrayList();
        }
        ProductThinPeriodExample example = new ProductThinPeriodExample();
        example.createCriteria().andProductCodeIn(productCodes).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<ProductThinPeriod> productThinPeriods = productThinPeriodMapper.selectByExample(example);
        return productThinPeriods;
    }

    @Override
    public List<ProductThinPeriod> getProductThinPeriodListByIds(List<String> ids) throws Exception {
        if(CollectionsUtil.isEmpty(ids)){
            return Lists.newArrayList();
        }
        ProductThinPeriodExample example = new ProductThinPeriodExample();
        example.createCriteria().andIdIn(ids).andStatusEqualTo(Constants.NORMAL_STATUS);
        return productThinPeriodMapper.selectByExample(example);
    }

    @Override
    public String addProductThinPeriod(String peiId,String productCode,String factoryName,String startCode) throws Exception {
        if(StringUtils.isEmpty(peiId)||StringUtils.isEmpty(productCode)||StringUtils.isEmpty(factoryName)){
            throw new Exception("peiId or productCode or factoryName is null");
        }
        ProductThinPeriodExample example = new ProductThinPeriodExample();
        example.createCriteria().andPeiIdEqualTo(peiId).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = productThinPeriodMapper.countByExample(example);
        if(existCount > 0){
            throw new Exception("data has exist");
        }
        ProductThinPeriod productThinPeriod = new ProductThinPeriod();
        String id = CommonUtil.generateRandomNum("thin-");
        productThinPeriod.setId(id);
        productThinPeriod.setFactoryName(factoryName);
        productThinPeriod.setProductCode(productCode);
        productThinPeriod.setPeiId(peiId);
        productThinPeriod.setStartCode(startCode);
        int count = productThinPeriodMapper.insertSelective(productThinPeriod);
        if(count < 1){
            throw new Exception("insert thin data error");
        }
        return "";
    }

    @Override
    public String updateBatchProductThinPeriod(List<ProductThinPeriod> productThinPeriods) throws Exception {
        if(CollectionUtils.isEmpty(productThinPeriods)){
            return "数组为空";
        }

        int count = productThinPeriodMapper.batchUpdateThinPeriods(productThinPeriods);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateProductThinPeriodLineTaskByIds(List<String> ids,String lineStatus) throws Exception {
        if(CollectionsUtil.isEmpty(ids)){
            return "";
        }
        ProductThinPeriodExample example = new ProductThinPeriodExample();
        example.createCriteria().andIdIn(ids);
        ProductThinPeriod update = new ProductThinPeriod();
        update.setLineStatus(lineStatus);
        int count = productThinPeriodMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<ProductThinPeriod> getProductThinPeriodBackList(QueryForm queryForm) throws Exception {
        int count = productThinPeriodMapper.getProductThinPeriodBackCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductThinPeriod> productThinPeriods = productThinPeriodMapper.getProductThinPeriodBackList(queryForm);
        productThinPeriods.get(0).setCount(count);
        return productThinPeriods;
    }
}
